package com.px.auditRecord.controller;

import java.util.List;
import java.util.stream.Stream;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.px.auditRecord.domain.AuditRecordDrone;
import com.px.auditRecord.service.IAuditRecordDroneService;
import com.px.auditRecord.vo.BlackWhiteDroneVo;
import com.px.common.utils.StringUtils;
import com.px.system.domain.FlightUser;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.px.framework.aes.AesSecurityParameter;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.px.common.annotation.Log;
import com.px.common.core.controller.BaseController;
import com.px.common.core.domain.AjaxResult;
import com.px.common.enums.BusinessType;
import com.px.auditRecord.domain.AuditBlackWhite;
import com.px.auditRecord.service.IAuditBlackWhiteService;
import com.px.common.core.page.TableDataInfo;

/**
 * 黑白名单控制Controller
 * 
 * @author WRJ
 * @date 2024-08
 */
@Api("黑白名单控制")
@RestController
@RequestMapping("/auditRecord/blackwhite")
public class AuditBlackWhiteController extends BaseController
{
    @Autowired
    private IAuditBlackWhiteService auditBlackWhiteService;
    @Autowired
    private IAuditRecordDroneService auditRecordDroneService;

    /**
     * 查询黑白名单控制列表
     */
    @ApiOperation("查询黑白名单控制列表")
    @PreAuthorize("@ss.hasPermi('auditRecord:blackwhite:query')")
    @GetMapping("/list")
    @AesSecurityParameter(outEncode = true)
    public TableDataInfo list(AuditBlackWhite auditBlackWhite)
    {
        startPage();
        List<AuditBlackWhite> list = auditBlackWhiteService.selectAuditBlackWhiteList(auditBlackWhite);
        return getDataTable(list);
    }

//    /**
//     * 导出黑白名单控制列表
//     */
//    @ApiOperation("导出黑白名单控制列表")
//    @PreAuthorize("@ss.hasPermi('auditRecord:blackwhite:query')")
//    @Log(title = "黑白名单控制", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    @AesSecurityParameter(outEncode = false)
//    public void export(HttpServletResponse response, AuditBlackWhite auditBlackWhite)
//    {
//        List<AuditBlackWhite> list = auditBlackWhiteService.selectAuditBlackWhiteList(auditBlackWhite);
//        ExcelUtil<AuditBlackWhite> util = new ExcelUtil<AuditBlackWhite>(AuditBlackWhite.class);
//        util.exportExcel(response, list, "黑白名单控制数据");
//    }

    /**
     * 获取黑白名单控制详细信息
     */
    @ApiOperation("获取黑白名单控制详细信息")
    @PreAuthorize("@ss.hasPermi('auditRecord:blackwhite:query')")
    @GetMapping(value = "/{id}")
    @AesSecurityParameter(outEncode = true)
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(auditBlackWhiteService.selectAuditBlackWhiteById(id));
    }

    /**
     * 新增黑白名单控制
     */
    @ApiOperation("新增黑白名单控制")
    @PreAuthorize("@ss.hasPermi('auditRecord:blackwhite:edit')")
    @Log(title = "黑白名单控制", businessType = BusinessType.INSERT)
    @PostMapping
    @AesSecurityParameter(outEncode = true)
    public AjaxResult add(@RequestBody AuditBlackWhite auditBlackWhite)
    {
        // 针对手动输入的SN校验,如果此SN已经在备案信息中存在,则不通过
        if ("3".equals(auditBlackWhite.getControlType())) {
            if (StringUtils.isNotEmpty(auditBlackWhite.getDroneSnExternal())) {
                for (String sn : auditBlackWhite.getDroneSnExternal().split(",")) {
                    AuditRecordDrone auditRecordDrone = new AuditRecordDrone();
                    auditRecordDrone.setSn(sn);
                    List<AuditRecordDrone> auditRecordDrones = null;
                    try {
                        auditRecordDrones = auditRecordDroneService.selectFlightList(auditRecordDrone);
                    } catch (Exception e) {
                    }
                    if (CollectionUtil.isNotEmpty(auditRecordDrones)) {
                        return AjaxResult.error("此SN已注册备案信息,请在设备列表选择后加入");
                    }
                    AuditBlackWhite external = new AuditBlackWhite();
                    BeanUtil.copyProperties(auditBlackWhite, external);
                    external.setDroneSn(sn);
                    auditBlackWhiteService.insertAuditBlackWhite(external);
                }
            }
        }

        // 支持批量新增
        if ("1".equals(auditBlackWhite.getControlType())
                && StringUtils.isNotEmpty(auditBlackWhite.getIdCard())) {
            String[] idCards = auditBlackWhite.getIdCard().split(",");
            Stream.of(idCards).forEach(i -> {
                auditBlackWhite.setIdCard(i);
                auditBlackWhiteService.insertAuditBlackWhite(auditBlackWhite);
            });
        }
        if ("2".equals(auditBlackWhite.getControlType())
                && StringUtils.isNotEmpty(auditBlackWhite.getPhoneNumber())) {
            String[] phones = auditBlackWhite.getPhoneNumber().split(",");
            Stream.of(phones).forEach(i -> {
                auditBlackWhite.setPhoneNumber(i);
                auditBlackWhiteService.insertAuditBlackWhite(auditBlackWhite);
            });
        }

        if ("3".equals(auditBlackWhite.getControlType()) && StringUtils.isNotEmpty(auditBlackWhite.getDroneSn())) {
            String[] sn = auditBlackWhite.getDroneSn().split(",");
            Stream.of(sn).forEach(i -> {
                auditBlackWhite.setDroneSn(i);
                auditBlackWhiteService.insertAuditBlackWhite(auditBlackWhite);
            });
        }

        return AjaxResult.success();
    }

//    /**
//     * 修改黑白名单控制
//     */
//    @ApiOperation("修改黑白名单控制")
//    @PreAuthorize("@ss.hasPermi('auditRecord:blackwhite:edit')")
//    @Log(title = "黑白名单控制", businessType = BusinessType.UPDATE)
//    @PutMapping
//    @AesSecurityParameter(outEncode = true)
//    public AjaxResult edit(@RequestBody AuditBlackWhite auditBlackWhite)
//    {
//        return toAjax(auditBlackWhiteService.updateAuditBlackWhite(auditBlackWhite));
//    }

    /**
     * 删除黑白名单控制
     */
    @ApiOperation("删除黑白名单控制")
    @PreAuthorize("@ss.hasPermi('auditRecord:blackwhite:remove')")
    @Log(title = "黑白名单控制", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    @AesSecurityParameter(outEncode = true)
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(auditBlackWhiteService.deleteAuditBlackWhiteByIds(ids));
    }

    @ApiOperation("可选设备列表")
    @PreAuthorize("@ss.hasPermi('auditRecord:blackwhite:query')")
    @GetMapping("droneList")
    @AesSecurityParameter(outEncode = true)
    public TableDataInfo droneList(AuditRecordDrone auditRecordDrone)
    {
        startPage();
        auditRecordDrone.setName(auditRecordDrone.getName());
        auditRecordDrone.setPhonenumber(auditRecordDrone.getName());
        auditRecordDrone.setIdcard(auditRecordDrone.getName());
        auditRecordDrone.setForName(auditRecordDrone.getName());
        auditRecordDrone.setAuditState("1");
        List<AuditRecordDrone> list = auditBlackWhiteService.selectDroneList(auditRecordDrone);
        return getDataTable(list);
    }

    /**
     * 查询黑白名单控制列表
     */
    @ApiOperation("查询黑白名单设备列表")
    @PreAuthorize("@ss.hasPermi('auditRecord:blackwhite:query')")
    @GetMapping("droneBlackWhiteList")
    @AesSecurityParameter(outEncode = true)
    public TableDataInfo devBlackWhiteList(BlackWhiteDroneVo droneVo)
    {
        startPage();
        List<BlackWhiteDroneVo> forRecordDrones = auditBlackWhiteService.selectForRecordDroneList(droneVo);
        return getDataTable(forRecordDrones);
    }

    @ApiOperation("查询可选用户列表")
    @PreAuthorize("@ss.hasPermi('auditRecord:blackwhite:query')")
    @GetMapping("fightList")
    @AesSecurityParameter(outEncode = true)
    public TableDataInfo fightList(FlightUser flightUser)
    {
        startPage();
        flightUser.setUserName(flightUser.getName());
        flightUser.setPhonenumber(flightUser.getName());
        flightUser.setIdcard(flightUser.getName());
        flightUser.setNickName(flightUser.getName());
        return getDataTable(auditBlackWhiteService.selectFlightUserListMap(flightUser));
    }

    @ApiOperation("查询可选账号列表")
    @PreAuthorize("@ss.hasPermi('auditRecord:blackwhite:query')")
    @GetMapping("accountList")
    @AesSecurityParameter(outEncode = true)
    public TableDataInfo accountList(FlightUser flightUser)
    {
        startPage();
        flightUser.setUserName(flightUser.getName());
        flightUser.setPhonenumber(flightUser.getName());
        flightUser.setIdcard(flightUser.getName());
        flightUser.setNickName(flightUser.getName());
        return getDataTable(auditBlackWhiteService.selectAccountUserListMap(flightUser));
    }
}
